#pragma once
#include "Room.h"
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;

class Building
{

private:
	//Uppgift 3.4
	vector<Room> v;
	vector<Room>::iterator RVI;
	string info;
	int id;
public:

	Building()
	{
		id = -1;
		info = " ";
	}

	Building(int id, string building, string info)
	{
		this->id = id;
		this->info = info;
	}

	vector<Room>& GetRooms()
	{
		return v;
	}

	//Uppgift 3.4
	void Add(Room r)
	{
		v.push_back(r);
	}

	void Search(int id)
	{
		Room temproom;
		temproom.SetID(id);

		auto RVI = find(v.begin(), v.end(), temproom);

		if (RVI != v.end())
		{
			cout << *RVI << endl;

		}

		else
		{
			cout << "Room could not be found!" << endl;
		}

	}

	void Delete(int id)
	{
		Room temproom;
		temproom.SetID(id);

		auto RVI = find(v.begin(), v.end(), temproom);
		if (RVI != v.end())
		{
			v.erase(RVI,RVI+1);
		}
	}

	string List()
	{
		ostringstream oss;
		for (int i = 0; i < v.size(); i++)
		{
			oss << v[i] << endl;
		}
		return oss.str();
	}

	//Uppgift 3.5
	void WriteToFile(string fileName)
	{
		ofstream fout;
		fout.open(fileName);
		fout << "Building= " << "(" << this->id << "," << this->info << ")" << endl;

		for (auto &r : v)
		{
			fout << "Room= " << r << endl;
		}

		fout.close();
	}

	//Uppgift 3.6

	void ReadFromFile(string fileName)
	{
		string str, strRow;
		ifstream fin;

		try
		{
			fin.open(fileName);
			v.clear();
			this->info = " ";
			

			while (getline(fin, strRow))
			{
				istringstream iss(strRow);
				getline(iss, str, '=');
				
				if (str.compare("Info") == 0)
				{
					getline(iss, this->info);
				}

				else if (str.compare("Room") == 0)
				{
					Room r;
					iss >> r;

					if (r.GetID() != -1)
					{
						v.push_back(r);
					}
				}
			}
			fin.close();
		}

		catch (exception e)
		{
			cerr << "Exception: " << fileName << endl;
			cerr << e.what() << endl;
		}
	}

	void Area()
	{
		double totalArea = 0;

		for (int i = 0; i < v.size(); i++)
		{
			totalArea += v[i].GetArea();
		}

		cout << "Total area: " << totalArea << "m2" << endl;

	}



	//Betyg 4

	void SetID(int id){ this->id = id; }
	int GetID() { return this->id; }

	friend bool operator==(const Building b1, const Building b2)
	{
		return b1.id == b2.id;
	}

	friend ostream& operator<<(ostream &out, const Building& Building)
	{
		out << "(";
		out << Building.id << ",";
		out << Building.info;
		out << ")";
		return out;
	}

	friend istream& operator>>(istream &in, Building& Building)
	{
		string s;
		getline(in, s, '(');
		getline(in, s, ',');
		istringstream iss(s);
		iss >> Building.id;
		getline(in, Building.info, ')');
		return in;
	}

	string PrintRoom(int j)
	{
		ostringstream oss;
		oss << "(" << v[j].GetID() << "," << v[j].GetArea() << "," << v[j].GetType() << ")" << endl;
		return oss.str();
	}


	void AddRoom()
	{
		Room temp;
		cout << "(id,area,type)" << endl;
		cin >> temp;
		v.push_back(temp);
	}

	double AreaTot()
	{
		double totalArea = 0;

		for (int i = 0; i < v.size(); i++)
		{
			totalArea += v[i].GetArea();
		}

		return totalArea;
	}

	
	~Building()
	{
	}
};

